﻿using System;
using System.Collections.Generic;
using System.Linq;
using DLM.Domain.Model;
using DLM.Domain.Repositories;
using DLM.Infrastructure.UnitOfWork;
using DLM.Infrastructure.Domain;
using Webdiyer.WebControls.Mvc;
using DLM.Domain.DTO;

namespace DLM.Data.EF.CodeFirst.Repositories
{
    public class InvitationRepository:Repository<Invitation,int>,IInvitationRepository
    {
        public InvitationRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        { }


        public override IEnumerable<Invitation> FindAll(System.Linq.Expressions.Expression<Func<Invitation, bool>> query, int index, int count)
        {
            return this.ActiveContext.Invitation.Where(query).OrderByDescending(i => i.InviteDate).ToPagedList(index, count);
        }

        public IEnumerable<Domain.DTO.InvitationDTO> FindInvitationDTO(string accountName, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name cannot be null!");
            }
            var result = from i in this.ActiveContext.Invitation
                         join u in this.ActiveContext.User
                         on i.UserID equals u.UserID
                         join p in this.ActiveContext.Party
                         on i.PartyID equals p.PartyID
                         join un in this.ActiveContext.User
                         on i.SenderID equals un.UserID
                         where u.AccountName == accountName
                         && i.IsConfirmed == false
                         select new InvitationDTO 
                         {
                             InvitationID = i.InvitationID,
                             UserID = i.UserID,
                             PartyID = i.PartyID,
                             SenderID = i.SenderID,
                             SenderAccount = i.SenderAccount,
                             IsConfirmed = i.IsConfirmed,
                             InviteDate = i.InviteDate,
                             PartyPhoto = p.PhotoUrl,
                             SenderPhoto = un.Photo

                         };
            return result.OrderByDescending(i => i.InviteDate).ToPagedList(pageIndex, pageSize);
        }


        public IEnumerable<InvitationHistoryDTO> FindInvitationHistoryDTO(string accountName, int pageIndex, int pageSize)
        {
            if (string.IsNullOrEmpty(accountName))
            {
                throw new ArgumentNullException("Account name cannot be null!");
            }
            var result = from i in this.ActiveContext.Invitation
                         join u in this.ActiveContext.User
                         on i.UserID equals u.UserID
                         join p in this.ActiveContext.Party
                         on i.PartyID equals p.PartyID
                         where i.SenderAccount == accountName
                         select new InvitationHistoryDTO
                         {
                             InvitationID = i.InvitationID,
                             UserID = i.UserID,
                             PartyID = i.PartyID,
                             SenderID = i.SenderID,
                             SenderAccount = i.SenderAccount,
                             IsConfirmed = i.IsConfirmed,
                             InviteDate = i.InviteDate,
                             PartyPhoto = p.PhotoUrl,
                             UserPhoto = u.Photo,
                             UserAccount = u.AccountName,
                             UserName = u.Name

                         };
            return result.OrderByDescending(i => i.InviteDate).ToPagedList(pageIndex, pageSize);
        }

    }
}
